<script setup>
import {ref} from 'vue'
import {dologin} from "@/api/login";

const loginForm = ref({
  username: '',
  password: ''
})
import {useRouter} from 'vue-router'
import {ElMessage} from "element-plus";

const router = useRouter();

const login = async () => {
  const res = await dologin(loginForm.value)
  //登录成功 进行路由的跳转
  if (res.code === 200) {
    const username = res.data.username;
    const id = res.data.id;
    const token = res.data.token;
    localStorage.setItem("username", username)
    localStorage.setItem("id", id)
    localStorage.setItem("token", token)
    router.push('/home')
  }else{
    ElMessage.error(res.message)
  }
}
</script>

<template>
  <div>
    <h1>欢迎来到酷鲨商城</h1>
    <el-form>
      <el-form-item label="用户名" style="width: 500px">
        <el-input v-model="loginForm.username"></el-input>
      </el-form-item>

      <el-form-item label="密码" style="width: 500px">
        <el-input v-model="loginForm.password"></el-input>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="login">登录</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<style scoped>

</style>
